/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import MouseSensorDriver.Calibrator;
import adc_lcard.ADCE140Driver;
import dynamixel.Actuator;
import dynamixel.EndoscopeGear;
import dynamixel.HostActuator;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFileChooser;
import javax.swing.Timer;
import jssc.SerialPortList;

/**
 *
 * @author Asko
 */
public class EndoscopFrame extends javax.swing.JFrame {

    String strBuf = "";
    HostActuator actuatorHost;
    int currentEngine = 0;
    String[] portList = SerialPortList.getPortNames();
    DefaultComboBoxModel<String> dynamixelModel = new DefaultComboBoxModel<>();
    final int actCnt = 4;
    Actuator[] actuators = new Actuator[actCnt];
    EndoscopeGear[] gears = new EndoscopeGear[actCnt];
    EndoscopeChannel[] eChannels = new EndoscopeChannel[actCnt];
    EndoscopeExpContainer expContainer = new EndoscopeExpContainer();
    ScriptAnalyzer analyzer;
    final String projectDirectory = "." + File.separator;
    //double maxPos[] = {43.01, 22.2657, 29.87, 150};
    //double minPos[] = {-36.65, -21.68, -31.65, 0};
    double maxPos[] = {45.0, 45.0, 45.0, 150};
    double minPos[] = {-45.0, -45.0, -45.0, 0};
    String[] nameUnitsPos = {"Grad", "Grad", "Grad", "mm"};
    String[] nameUnitsTorque = {"N*m", "N*m", "N*m", "N"};
    int[] adcChannelNums = {13, 12, 14, 15};
    int[] adcChannelNumsOS = {30, 31, 32, 33};
    int baudRate = 57142;
    int sI = 0;
    int it = 0;
    GraphObserver go = new GraphObserver();
    Timer timer = new Timer(300, new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            //System.out.println("Action performed " + it++);
//            strBuf = "" + Calibrator.get().
//                    getData(Calibrator.ES_MOTOR_POS_1 + currentEngine*2,
//                    actuators[currentEngine].getCurPos());
//            strBuf = "" + gears[currentEngine].getPosition();
//            System.out.println("Str buf: "+ strBuf);
//            j_label_cur_pos.setText(strBuf.substring(0, strBuf.length() > 4 ? 5 : strBuf.length()));
//            strBuf = "" + gears[currentEngine].getTorque();
//            System.out.println("Str buf: "+ strBuf);
//            j_label_torque.setText(strBuf.substring(0, strBuf.length() > 4 ? 5 : strBuf.length()));
//
//            if ((actuatorHost.getErrorInlastMsg() & HostActuator.INPUT_VOLTAGE_ERROR) != 0) {
//                panel_1.setBackground(Color.red);
//            } else {
//                panel_1.setBackground(Color.white);
//            }
//            if ((actuatorHost.getErrorInlastMsg() & HostActuator.ANGLE_LIMIT_ERROR) != 0) {
//                panel_2.setBackground(Color.red);
//            } else {
//                panel_2.setBackground(Color.white);
//            }
//            if ((actuatorHost.getErrorInlastMsg() & HostActuator.OVERHEATING_ERROR) != 0) {
//                panel_3.setBackground(Color.red);
//            } else {
//                panel_3.setBackground(Color.white);
//            }
//            if ((actuatorHost.getErrorInlastMsg() & HostActuator.RANGE_ERROR) != 0) {
//                panel_4.setBackground(Color.red);
//            } else {
//                panel_4.setBackground(Color.white);
//            }
//            if ((actuatorHost.getErrorInlastMsg() & HostActuator.CHECKSUM_ERROR) != 0) {
//                panel_5.setBackground(Color.red);
//            } else {
//                panel_5.setBackground(Color.white);
//            }
//            if ((actuatorHost.getErrorInlastMsg() & HostActuator.OVERLOAD_ERROR) != 0) {
//                panel_6.setBackground(Color.red);
//            } else {
//                panel_6.setBackground(Color.white);
//            }
//            if ((actuatorHost.getErrorInlastMsg() & HostActuator.INSTRUCTION_ERROR) != 0) {
//                panel_7.setBackground(Color.red);
//            } else {
//                panel_7.setBackground(Color.white);
//            }
        }
    });

    /**
     * Creates new form EndoscopFrame
     */
    public EndoscopFrame() {
        for (int i = 0; i < portList.length; i++) {
            dynamixelModel.addElement(portList[i]);
        }
        if (!ADCE140Driver.init()) {
            System.out.println("ADC failed  to initialize. Try again or reboot.");
            System.exit(1);
        }
        actuatorHost = new HostActuator(baudRate);
        for (int i = 0; i < actCnt; i++) {
            actuators[i] = new Actuator(actuatorHost, i + 1);
            gears[i] = new EndoscopeGear(actuators[i], nameUnitsPos[i],
                    nameUnitsTorque[i], maxPos[i], minPos[i]);
            eChannels[i] = new EndoscopeChannel(gears[i], adcChannelNums[i],
                    Calibrator.ES_ADC_CHANELL_1 + i,
                    Calibrator.ES_TOR_ADC_CHANELL_1 + i);
        }
        analyzer = new ScriptAnalyzer("", expContainer, eChannels);
        timer.start();
        System.out.println("Init done");

        initComponents();
        this.setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        j_popup = new javax.swing.JPopupMenu();
        j_menu_item = new javax.swing.JMenuItem();
        jPanel5 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        j_cb2 = new javax.swing.JComboBox();
        jPanel6 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        j_tf1 = new javax.swing.JTextField();
        j_label_units_name_1 = new javax.swing.JLabel();
        j_lb_min = new javax.swing.JLabel();
        j_lb_max = new javax.swing.JLabel();
        jPanel8 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        j_label_torque_value = new javax.swing.JLabel();
        j_label_units_name = new javax.swing.JLabel();
        j_torgue_slider = new javax.swing.JSlider();
        jButton3 = new javax.swing.JButton();
        jPanel9 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        panel_7 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        panel_6 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        panel_5 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        panel_4 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        panel_3 = new javax.swing.JPanel();
        jLabel12 = new javax.swing.JLabel();
        panel_2 = new javax.swing.JPanel();
        jLabel13 = new javax.swing.JLabel();
        panel_1 = new javax.swing.JPanel();
        jPanel10 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jPanel14 = new javax.swing.JPanel();
        j_label_cur_pos = new javax.swing.JLabel();
        j_label_pos_limit = new javax.swing.JLabel();
        j_label_torque = new javax.swing.JLabel();
        j_label_torque_unit = new javax.swing.JLabel();
        j_tf_prop_coef = new javax.swing.JTextField();
        j_tf_offset = new javax.swing.JTextField();
        j_tf_prop_coef_torque = new javax.swing.JTextField();
        j_tf_offset_torque = new javax.swing.JTextField();
        jPanel11 = new javax.swing.JPanel();
        jPanel13 = new javax.swing.JPanel();
        jButton7 = new javax.swing.JButton();
        j_button_delete_row = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        j_button_to_graph_pos = new javax.swing.JButton();
        j_button_to_graph_torque = new javax.swing.JButton();
        jButton8 = new javax.swing.JButton();
        jButton9 = new javax.swing.JButton();
        j_b_save = new javax.swing.JButton();
        j_b_load = new javax.swing.JButton();
        jtb_post = new javax.swing.JToggleButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        j_text_area = new javax.swing.JTextArea();
        jRotateArrow1 = new org.min.gui.JRotateArrow();

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        j_menu_item.setText("Открыть сценарий");
        j_menu_item.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                j_menu_itemActionPerformed(evt);
            }
        });
        j_popup.add(j_menu_item);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel2.setText("Actuator");

        j_cb2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Поперечное вращение", "Вращение вокруг оси", "Продольное вращение", "Поступательное движениe" }));
        j_cb2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                j_cb2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(j_cb2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(j_cb2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButton2.setText("GoToPos");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        j_label_units_name_1.setText("mm");

        j_lb_min.setText("0");

        j_lb_max.setText("0");

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel7Layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(j_tf1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(j_label_units_name_1))
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addComponent(j_lb_min)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(j_lb_max)
                        .addGap(0, 0, Short.MAX_VALUE))))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(j_tf1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(j_label_units_name_1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(j_lb_min)
                    .addComponent(j_lb_max))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel3.setText("Torque limit");

        j_label_torque_value.setText("0");

        j_label_units_name.setText("N*m");

        j_torgue_slider.setMaximum(1023);
        j_torgue_slider.setValue(0);
        j_torgue_slider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                j_torgue_sliderStateChanged(evt);
            }
        });

        jButton3.setText("Apply");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel8Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(j_label_torque_value)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(j_label_units_name)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel8Layout.createSequentialGroup()
                        .addComponent(j_torgue_slider, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton3)))
                .addContainerGap())
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel8Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(j_label_torque_value)
                    .addComponent(j_label_units_name))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(j_torgue_slider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jPanel1.setLayout(new java.awt.GridLayout(0, 2));

        jLabel7.setText("Instruction Error");
        jPanel1.add(jLabel7);

        javax.swing.GroupLayout panel_7Layout = new javax.swing.GroupLayout(panel_7);
        panel_7.setLayout(panel_7Layout);
        panel_7Layout.setHorizontalGroup(
            panel_7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 135, Short.MAX_VALUE)
        );
        panel_7Layout.setVerticalGroup(
            panel_7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 26, Short.MAX_VALUE)
        );

        jPanel1.add(panel_7);

        jLabel8.setText("Overload Error");
        jPanel1.add(jLabel8);

        javax.swing.GroupLayout panel_6Layout = new javax.swing.GroupLayout(panel_6);
        panel_6.setLayout(panel_6Layout);
        panel_6Layout.setHorizontalGroup(
            panel_6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 135, Short.MAX_VALUE)
        );
        panel_6Layout.setVerticalGroup(
            panel_6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 26, Short.MAX_VALUE)
        );

        jPanel1.add(panel_6);

        jLabel9.setText("CheckSum Error");
        jPanel1.add(jLabel9);

        javax.swing.GroupLayout panel_5Layout = new javax.swing.GroupLayout(panel_5);
        panel_5.setLayout(panel_5Layout);
        panel_5Layout.setHorizontalGroup(
            panel_5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 135, Short.MAX_VALUE)
        );
        panel_5Layout.setVerticalGroup(
            panel_5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 26, Short.MAX_VALUE)
        );

        jPanel1.add(panel_5);

        jLabel10.setText("Range Error");
        jPanel1.add(jLabel10);

        javax.swing.GroupLayout panel_4Layout = new javax.swing.GroupLayout(panel_4);
        panel_4.setLayout(panel_4Layout);
        panel_4Layout.setHorizontalGroup(
            panel_4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 135, Short.MAX_VALUE)
        );
        panel_4Layout.setVerticalGroup(
            panel_4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 26, Short.MAX_VALUE)
        );

        jPanel1.add(panel_4);

        jLabel11.setText("Overheating Error");
        jPanel1.add(jLabel11);

        javax.swing.GroupLayout panel_3Layout = new javax.swing.GroupLayout(panel_3);
        panel_3.setLayout(panel_3Layout);
        panel_3Layout.setHorizontalGroup(
            panel_3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 135, Short.MAX_VALUE)
        );
        panel_3Layout.setVerticalGroup(
            panel_3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 26, Short.MAX_VALUE)
        );

        jPanel1.add(panel_3);

        jLabel12.setText("Angle Limit Error");
        jPanel1.add(jLabel12);

        javax.swing.GroupLayout panel_2Layout = new javax.swing.GroupLayout(panel_2);
        panel_2.setLayout(panel_2Layout);
        panel_2Layout.setHorizontalGroup(
            panel_2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 135, Short.MAX_VALUE)
        );
        panel_2Layout.setVerticalGroup(
            panel_2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 26, Short.MAX_VALUE)
        );

        jPanel1.add(panel_2);

        jLabel13.setText("Input Voltage Error");
        jPanel1.add(jLabel13);

        javax.swing.GroupLayout panel_1Layout = new javax.swing.GroupLayout(panel_1);
        panel_1.setLayout(panel_1Layout);
        panel_1Layout.setHorizontalGroup(
            panel_1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 135, Short.MAX_VALUE)
        );
        panel_1Layout.setVerticalGroup(
            panel_1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 26, Short.MAX_VALUE)
        );

        jPanel1.add(panel_1);

        javax.swing.GroupLayout jPanel9Layout = new javax.swing.GroupLayout(jPanel9);
        jPanel9.setLayout(jPanel9Layout);
        jPanel9Layout.setHorizontalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
            .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel9Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        jPanel9Layout.setVerticalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 212, Short.MAX_VALUE)
            .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel9Layout.createSequentialGroup()
                    .addGap(12, 12, 12)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                    .addContainerGap()))
        );

        jLabel6.setText("Torque");

        jLabel4.setText("Position");

        jLabel5.setText("PropPosCoef");

        jLabel14.setText("PosOffset");

        jLabel15.setText("PropTorqueCoef");

        jLabel16.setText("TorqueOffset");

        j_label_cur_pos.setText("0");

        j_label_pos_limit.setText("mm");

        j_label_torque.setText("0");

        j_label_torque_unit.setText("mm");

        j_tf_prop_coef.setText("0");
        j_tf_prop_coef.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                j_tf_prop_coefActionPerformed(evt);
            }
        });

        j_tf_offset.setText("0");

        j_tf_prop_coef_torque.setText("0");

        j_tf_offset_torque.setText("0");

        javax.swing.GroupLayout jPanel14Layout = new javax.swing.GroupLayout(jPanel14);
        jPanel14.setLayout(jPanel14Layout);
        jPanel14Layout.setHorizontalGroup(
            jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel14Layout.createSequentialGroup()
                .addGroup(jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(j_tf_offset_torque, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE)
                    .addComponent(j_tf_prop_coef_torque, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(j_tf_offset, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel14Layout.createSequentialGroup()
                        .addComponent(j_label_cur_pos)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(j_label_pos_limit))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel14Layout.createSequentialGroup()
                        .addComponent(j_label_torque)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(j_label_torque_unit))
                    .addComponent(j_tf_prop_coef, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel14Layout.setVerticalGroup(
            jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel14Layout.createSequentialGroup()
                .addGroup(jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(j_label_cur_pos)
                    .addComponent(j_label_pos_limit))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel14Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(j_label_torque)
                    .addComponent(j_label_torque_unit))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(j_tf_prop_coef, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(j_tf_offset, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(16, 16, 16)
                .addComponent(j_tf_prop_coef_torque, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(j_tf_offset_torque, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 30, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel10Layout = new javax.swing.GroupLayout(jPanel10);
        jPanel10.setLayout(jPanel10Layout);
        jPanel10Layout.setHorizontalGroup(
            jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel10Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel10Layout.createSequentialGroup()
                        .addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel4)
                            .addComponent(jLabel16)
                            .addComponent(jLabel5)
                            .addComponent(jLabel14))
                        .addGap(24, 24, 24))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel10Layout.createSequentialGroup()
                        .addComponent(jLabel15)
                        .addGap(18, 18, 18)))
                .addComponent(jPanel14, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel10Layout.setVerticalGroup(
            jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel10Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel14, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel10Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel5)
                        .addGap(19, 19, 19)
                        .addComponent(jLabel14)
                        .addGap(26, 26, 26)
                        .addComponent(jLabel15)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel16)
                        .addGap(0, 17, Short.MAX_VALUE)))
                .addContainerGap())
        );

        jButton7.setBackground(new java.awt.Color(238, 0, 0));
        jButton7.setText("removeAll");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        j_button_delete_row.setBackground(new java.awt.Color(238, 0, 0));
        j_button_delete_row.setText("DeleteRow");
        j_button_delete_row.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                j_button_delete_rowActionPerformed(evt);
            }
        });

        jButton4.setText("Export");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("DoScript");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("ViewObserver");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        j_button_to_graph_pos.setText("toGraphPos");
        j_button_to_graph_pos.setEnabled(false);
        j_button_to_graph_pos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                j_button_to_graph_posActionPerformed(evt);
            }
        });

        j_button_to_graph_torque.setText("toGraphTorque");
        j_button_to_graph_torque.setEnabled(false);
        j_button_to_graph_torque.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                j_button_to_graph_torqueActionPerformed(evt);
            }
        });

        jButton8.setText("posEA");
        jButton8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton8ActionPerformed(evt);
            }
        });

        jButton9.setText("torEA");
        jButton9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton9ActionPerformed(evt);
            }
        });

        j_b_save.setText("Save");
        j_b_save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                j_b_saveActionPerformed(evt);
            }
        });

        j_b_load.setText("Load");
        j_b_load.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                j_b_loadActionPerformed(evt);
            }
        });

        jtb_post.setSelected(true);
        jtb_post.setText("Post");

        javax.swing.GroupLayout jPanel13Layout = new javax.swing.GroupLayout(jPanel13);
        jPanel13.setLayout(jPanel13Layout);
        jPanel13Layout.setHorizontalGroup(
            jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel13Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton5)
                    .addComponent(j_button_delete_row)
                    .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel13Layout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(jPanel13Layout.createSequentialGroup()
                                .addComponent(jButton6)
                                .addGap(18, 18, 18)
                                .addComponent(j_button_to_graph_pos, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel13Layout.createSequentialGroup()
                                .addComponent(jtb_post, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(j_button_to_graph_torque)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(jPanel13Layout.createSequentialGroup()
                        .addComponent(jButton7, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(j_b_load)
                        .addGap(18, 18, 18)
                        .addComponent(j_b_save)))
                .addContainerGap())
        );
        jPanel13Layout.setVerticalGroup(
            jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel13Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel13Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton4)
                        .addGap(44, 44, 44)
                        .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(j_button_delete_row)
                            .addComponent(jButton7)
                            .addComponent(j_b_save)
                            .addComponent(j_b_load)))
                    .addGroup(jPanel13Layout.createSequentialGroup()
                        .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton8)
                            .addComponent(j_button_to_graph_pos)
                            .addComponent(jButton6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel13Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton9)
                            .addComponent(j_button_to_graph_torque)
                            .addComponent(jtb_post))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel11Layout = new javax.swing.GroupLayout(jPanel11);
        jPanel11.setLayout(jPanel11Layout);
        jPanel11Layout.setHorizontalGroup(
            jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel11Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel13, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel11Layout.setVerticalGroup(
            jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel13, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        jTable1.setModel(expContainer);
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        j_text_area.setColumns(20);
        j_text_area.setRows(5);
        j_text_area.setComponentPopupMenu(j_popup);
        jScrollPane2.setViewportView(j_text_area);

        javax.swing.GroupLayout jRotateArrow1Layout = new javax.swing.GroupLayout(jRotateArrow1);
        jRotateArrow1.setLayout(jRotateArrow1Layout);
        jRotateArrow1Layout.setHorizontalGroup(
            jRotateArrow1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 216, Short.MAX_VALUE)
        );
        jRotateArrow1Layout.setVerticalGroup(
            jRotateArrow1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jPanel10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jPanel9, javax.swing.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jRotateArrow1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1)))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jPanel11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jRotateArrow1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 8, Short.MAX_VALUE))
                    .addComponent(jScrollPane2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        int selIndx = j_cb2.getSelectedIndex();
        if (selIndx != -1) {
            gears[selIndx].setPosition(Double.valueOf(j_tf1.getText()));
//            actuators[j_cb2.getSelectedIndex()].setGoalPosition(Integer.
//                    valueOf(j_tf1.getText()));
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        int selIndx = j_cb2.getSelectedIndex();
        if (selIndx != -1) {
            actuators[j_cb2.getSelectedIndex()].
                    setTorqueLimit(j_torgue_slider.getValue());
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton3ActionPerformed

    private void j_cb2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_j_cb2ActionPerformed
        int selIndx = j_cb2.getSelectedIndex();
        if (selIndx != -1) {

            System.out.println("Selected indx: " + selIndx);
//            j_label_units_name.setText("" + gears[selIndx].getUnitTorque());
//            j_label_units_name_1.setText("" + gears[selIndx].getUnitPosition());
//            j_lb_max.setText("" + gears[selIndx].getMaxPos());
//            j_lb_min.setText("" + gears[selIndx].getMinPos());
//            j_label_torque_unit.setText("" + gears[selIndx].getUnitTorque());
//            j_label_pos_limit.setText("" + gears[selIndx].getUnitPosition());
            currentEngine = selIndx;
        }

    }//GEN-LAST:event_j_cb2ActionPerformed

    private void j_torgue_sliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_j_torgue_sliderStateChanged
        sI = j_cb2.getSelectedIndex();
        if (sI != -1) {
            strBuf = "" + Calibrator.get().getData(sI * 2 + 1
                    + Calibrator.ES_MOTOR_POS_1,
                    j_torgue_slider.getValue());
            j_label_torque_value.setText(strBuf.substring(0, strBuf.length()
                    > 4 ? 5 : strBuf.length()));
        }
    }//GEN-LAST:event_j_torgue_sliderStateChanged

    private void j_button_delete_rowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_j_button_delete_rowActionPerformed
        int r = jTable1.getSelectedRow();
        //   System.out.println("Selected row -> " +  r);
        if (r != -1) {
            //System.out.println("getSelectedRow "+jTable1.getSelectedRow()+" col1"+jTable1.getSelectedColumns()[0]);
            if (jTable1.getSelectedColumnCount() > 1) {
                int ind = jTable1.getSelectedRow();
                int cnt_del = jTable1.getSelectedRowCount();
                for (int i = 0; i < cnt_del; i++) {
                    expContainer.remove(ind);
                }
            } else {
                expContainer.remove(r);
            }
        }
    }//GEN-LAST:event_j_button_delete_rowActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        JFileChooser fc = new JFileChooser("Выберите файл для сохранения");
        fc.setDialogType(JFileChooser.SAVE_DIALOG);
        int res = fc.showSaveDialog(this);
        if (res == JFileChooser.APPROVE_OPTION) {
            try {
                File file = fc.getSelectedFile().getAbsolutePath().contains(".")
                        ? fc.getSelectedFile() : new File(fc.getSelectedFile().
                        getAbsolutePath() + ".csv");
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(expContainer.getCSV().getBytes("windows-1251"));
                fos.close();
            } catch (IOException ex) {
                Logger.getLogger(ControlFrrame.class.getName()).
                        log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed

//        new Thread(new Runnable() {
//
//            @Override
//            public void run() {
        analyzer.setSource(j_text_area.getText());
        analyzer.setAirship(jtb_post.isSelected());
        analyzer.startAnalyze();

//            }
//        }).start();
//        
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        int sel = jTable1.getSelectedRow();
        if (sel != -1) {
            j_tf_prop_coef.setText("" + expContainer.get(sel).getCoefPropCalibPosAdc());
            j_tf_offset.setText("" + expContainer.get(sel).getCoefOffsetCalibPosAdc());
            j_tf_prop_coef_torque.setText("" + expContainer.get(sel).getCoefPropCalibTorqueAdc());
            j_tf_offset_torque.setText("" + expContainer.get(sel).getCoefOffsetCalibTorqueAdc());
            if (expContainer.get(sel).getSize() > 0) {
                j_button_to_graph_pos.setEnabled(true);
                j_button_to_graph_torque.setEnabled(true);
            } else {
                j_button_to_graph_pos.setEnabled(false);
                j_button_to_graph_torque.setEnabled(false);
            }
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_jTable1MouseClicked

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        go.setVisible(true);
    }//GEN-LAST:event_jButton6ActionPerformed

    private void j_button_to_graph_posActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_j_button_to_graph_posActionPerformed
        go.removeAllPlots();
        int sel = jTable1.getSelectedRow();
        if (sel > -1) {
            if (expContainer.get(sel).getSize() > 0) {
                go.addPlot("Позиция двигателя", expContainer.get(sel).getTime(),
                        expContainer.get(sel).getPosEng());
                go.addPlot("Позиция АЦП", expContainer.get(sel).getTime(),
                        expContainer.get(sel).getPosAdc());
            }
        }
    }//GEN-LAST:event_j_button_to_graph_posActionPerformed

    private void j_button_to_graph_torqueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_j_button_to_graph_torqueActionPerformed
        go.removeAllPlots();
        int sel = jTable1.getSelectedRow();
        if (sel > -1) {
            if (expContainer.get(sel).getSize() > 0) {
                go.addPlot("Момент двигателя", expContainer.get(sel).getTime(),
                        expContainer.get(sel).getTorEng());
                go.addPlot("Момент АЦП", expContainer.get(sel).getTime(),
                        expContainer.get(sel).getTorAdc());
            }
        }// TODO add your handling code here:
    }//GEN-LAST:event_j_button_to_graph_torqueActionPerformed

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        expContainer.removeAll();
    }//GEN-LAST:event_jButton7ActionPerformed

    private void j_menu_itemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_j_menu_itemActionPerformed
        JFileChooser jf = new JFileChooser();
        //jf.setCurrentDirectory(new File("C:\\Users\\Asko\\Desktop\\Scripts"));
        int res = jf.showOpenDialog(this);
        if (res == JFileChooser.APPROVE_OPTION) {
            try {
                FileInputStream fis = new FileInputStream(jf.getSelectedFile());
                String buf = "";
                while ((res = fis.read()) != -1) {
                    buf += (char) res;
                }
                fis.close();
                j_text_area.setText(buf);
            } catch (IOException ex) {
                Logger.getLogger(EndoscopFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_j_menu_itemActionPerformed

    private void j_tf_prop_coefActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_j_tf_prop_coefActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_j_tf_prop_coefActionPerformed

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton8ActionPerformed

        go.removeAllPlots();
        int sel = jTable1.getSelectedRow();
        if (sel > -1) {
            if (expContainer.get(sel).getSize() > 0) {
                go.addPlot("Позиция АЦП относительно привода",
                        expContainer.get(sel).getPosEng(),
                        expContainer.get(sel).getPosAdc());
            }
        }
    }//GEN-LAST:event_jButton8ActionPerformed

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton9ActionPerformed
        go.removeAllPlots();
        int sel = jTable1.getSelectedRow();
        if (sel > -1) {
            if (expContainer.get(sel).getSize() > 0) {
                go.addPlot("Момент АЦП относительно привода",
                        expContainer.get(sel).getTorEng(),
                        expContainer.get(sel).getTorAdc());
            }
        }
    }//GEN-LAST:event_jButton9ActionPerformed

    private void j_b_saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_j_b_saveActionPerformed
    }//GEN-LAST:event_j_b_saveActionPerformed

    private void j_b_loadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_j_b_loadActionPerformed
       
        
    }//GEN-LAST:event_j_b_loadActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(EndoscopFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EndoscopFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EndoscopFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EndoscopFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new EndoscopFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JButton jButton8;
    private javax.swing.JButton jButton9;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel13;
    private javax.swing.JPanel jPanel14;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private org.min.gui.JRotateArrow jRotateArrow1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton j_b_load;
    private javax.swing.JButton j_b_save;
    private javax.swing.JButton j_button_delete_row;
    private javax.swing.JButton j_button_to_graph_pos;
    private javax.swing.JButton j_button_to_graph_torque;
    private javax.swing.JComboBox j_cb2;
    private javax.swing.JLabel j_label_cur_pos;
    private javax.swing.JLabel j_label_pos_limit;
    private javax.swing.JLabel j_label_torque;
    private javax.swing.JLabel j_label_torque_unit;
    private javax.swing.JLabel j_label_torque_value;
    private javax.swing.JLabel j_label_units_name;
    private javax.swing.JLabel j_label_units_name_1;
    private javax.swing.JLabel j_lb_max;
    private javax.swing.JLabel j_lb_min;
    private javax.swing.JMenuItem j_menu_item;
    private javax.swing.JPopupMenu j_popup;
    private javax.swing.JTextArea j_text_area;
    private javax.swing.JTextField j_tf1;
    private javax.swing.JTextField j_tf_offset;
    private javax.swing.JTextField j_tf_offset_torque;
    private javax.swing.JTextField j_tf_prop_coef;
    private javax.swing.JTextField j_tf_prop_coef_torque;
    private javax.swing.JSlider j_torgue_slider;
    private javax.swing.JToggleButton jtb_post;
    private javax.swing.JPanel panel_1;
    private javax.swing.JPanel panel_2;
    private javax.swing.JPanel panel_3;
    private javax.swing.JPanel panel_4;
    private javax.swing.JPanel panel_5;
    private javax.swing.JPanel panel_6;
    private javax.swing.JPanel panel_7;
    // End of variables declaration//GEN-END:variables
}
